package com.cn.jinl.service;

import com.cn.jinl.api.IAbandonMaterialHandleService;
import com.cn.jinl.dao.AbandonMaterialHandleMapper;
import com.cn.jinl.dao.AbandonMaterialMapper;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.NumberUtil;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AbandonMaterialHandleServiceImpl implements IAbandonMaterialHandleService {

    @Autowired
    private AbandonMaterialHandleMapper abandonMaterialHandleMapper;

    @Autowired
    private AbandonMaterialMapper abandonMaterialMapper;

    @Override
    public List<AbandonMaterialHandle> getAbandonMaterialHandleList(QueryForm queryForm) throws Exception {
        int count = abandonMaterialHandleMapper.selectAbandonMaterialHandleListCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<AbandonMaterialHandle> handles = abandonMaterialHandleMapper.selectAbandonMaterialHandleList(queryForm);
        handles.get(0).setCount(count);
        return handles;
    }

    @Override
    public List<AbandonVo> getAbandonVoList() throws Exception {
        QueryForm queryForm = new QueryForm();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String year = simpleDateFormat.format(new Date());
        String lastYear = (Integer.parseInt(year) - 1) + "";
        String lastYearMothStart = lastYear + "-01-01";
        String lastYearMothEnd = lastYear + "-12-31 23:59:59";
        queryForm.setStartTime(lastYearMothStart);
        queryForm.setEndTime(lastYearMothEnd);
        List<AbandonMaterial> abandonPreMaterials = abandonMaterialMapper.selectYearTotalWeight(queryForm);
        List<AbandonMaterialHandle> abandonPreMaterialHandles = abandonMaterialHandleMapper.selectYearTotalOutWeight(queryForm);
        AbandonVo vo1 = new AbandonVo();
        vo1.setAbandonType("1");
        AbandonVo vo2 = new AbandonVo();
        vo2.setAbandonType("2");
        AbandonVo vo3 = new AbandonVo();
        vo3.setAbandonType("3");
        AbandonVo vo4 = new AbandonVo();
        vo4.setAbandonType("4");
        Map<String, AbandonVo> resultMaps = Maps.newHashMap();
        resultMaps.put("1", vo1);
        resultMaps.put("2", vo2);
        resultMaps.put("3", vo3);
        resultMaps.put("4", vo4);
        Map<String, Double> mapPreIn = Maps.newHashMap();
        Map<String, Double> mapPreOut = Maps.newHashMap();
        for (AbandonMaterial abandon : abandonPreMaterials) {
            String abandonType = abandon.getAbandonType();
            if (mapPreIn.containsKey(abandonType)) {
                Double value = mapPreIn.get(abandonType);
                mapPreIn.put(abandonType, value + Double.parseDouble(abandon.getWeight()));
            } else {
                mapPreIn.put(abandonType, Double.parseDouble(abandon.getWeight()));
            }
        }
        for (AbandonMaterialHandle handle : abandonPreMaterialHandles) {
            String abandonType = handle.getAbandonType();
            if (mapPreOut.containsKey(abandonType)) {
                Double value = mapPreOut.get(abandonType);
                mapPreOut.put(abandonType, value + Double.parseDouble(handle.getOutWeight()));
            } else {
                mapPreOut.put(abandonType, Double.parseDouble(handle.getOutWeight()));
            }
        }

        String todayYearStart = year + "-01-01";
        queryForm.setStartTime(todayYearStart);
        List<AbandonMaterial> abandonMaterials = abandonMaterialMapper.selectYearAllTotalWeight(queryForm);
        List<AbandonMaterialHandle> abandonMaterialHandles = abandonMaterialHandleMapper.selectYearAllTotalOutWeight(queryForm);


        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String yearMothDay = simpleDateFormat1.format(new Date());
        String splits[] = yearMothDay.split("-");
        String yearMoth = splits[0] + "-" + splits[1];
        String todayYear = splits[0];
        for (AbandonMaterial abandon : abandonMaterials) {
            String abandonType = abandon.getAbandonType();
            String abandonTime = simpleDateFormat1.format(abandon.getCreateTime());
            String splitTemps[] = abandonTime.split("-");
            Double value = Double.parseDouble(abandon.getWeight());
            if (StringUtils.equals(yearMothDay, abandonTime)) {
                Double todayWeight = Double.parseDouble(resultMaps.get(abandonType).getTodayInWeight()) + value;
                resultMaps.get(abandonType).setTodayInWeight(todayWeight + "");
            }
            if (StringUtils.equals(yearMoth, splitTemps[0] + "-" + splitTemps[1])) {
                Double todayMothWeight = Double.parseDouble(resultMaps.get(abandonType).getTodayMothInWeight()) + value;
                resultMaps.get(abandonType).setTodayMothInWeight(todayMothWeight + "");
            }
            if (StringUtils.equals(todayYear, splitTemps[0])) {
                Double todayYearWeight = Double.parseDouble(resultMaps.get(abandonType).getTodayYearInWeight()) + value;
                resultMaps.get(abandonType).setTodayYearInWeight(todayYearWeight + "");
            }
        }
        for (AbandonMaterialHandle handle : abandonMaterialHandles) {
            String abandonType = handle.getAbandonType();
            String abandonTime = simpleDateFormat1.format(handle.getCreateTime());
            String splitTemps[] = abandonTime.split("-");
            Double value = Double.parseDouble(handle.getOutWeight());
            if (StringUtils.equals(yearMothDay, abandonTime)) {
                Double todayWeight = Double.parseDouble(resultMaps.get(abandonType).getTodayOutWeight()) + value;
                resultMaps.get(abandonType).setTodayOutWeight(todayWeight + "");
            }
            if (StringUtils.equals(yearMoth, splitTemps[0] + "-" + splitTemps[1])) {
                Double todayMothWeight = Double.parseDouble(resultMaps.get(abandonType).getTodayMothOutWeight()) + value;
                resultMaps.get(abandonType).setTodayMothOutWeight(todayMothWeight + "");
            }
            if (StringUtils.equals(todayYear, splitTemps[0])) {
                Double todayYearWeight = Double.parseDouble(resultMaps.get(abandonType).getTodayYearOutWeight()) + value;
                resultMaps.get(abandonType).setTodayYearOutWeight(todayYearWeight + "");
            }
        }

        List<AbandonVo> result = Lists.newArrayList();
        for (String key : resultMaps.keySet()) {
            double preValue = 0.00;
            if (mapPreIn.containsKey(key)) {
                preValue = mapPreIn.get(key);
            }
            if (mapPreOut.containsKey(key)) {
                preValue = preValue - mapPreOut.get(key);
            }

            resultMaps.get(key).setPreYearWeight(NumberUtil.getPoint(preValue, 2));
            resultMaps.get(key).setTodayYearOutWeight(NumberUtil.getPoint(resultMaps.get(key).getTodayYearOutWeight(), 2));
            resultMaps.get(key).setTodayYearInWeight(NumberUtil.getPoint(resultMaps.get(key).getTodayYearInWeight(), 2));
            resultMaps.get(key).setTodayMothInWeight(NumberUtil.getPoint(resultMaps.get(key).getTodayMothInWeight(), 2));
            resultMaps.get(key).setTodayMothOutWeight(NumberUtil.getPoint(resultMaps.get(key).getTodayMothOutWeight(), 2));
            resultMaps.get(key).setTodayInWeight(NumberUtil.getPoint(resultMaps.get(key).getTodayInWeight(), 2));
            resultMaps.get(key).setTodayOutWeight(NumberUtil.getPoint(resultMaps.get(key).getTodayOutWeight(), 2));
            double currentValue = preValue + Double.parseDouble(resultMaps.get(key).getTodayYearInWeight()) - Double.parseDouble(resultMaps.get(key).getTodayYearOutWeight());
            resultMaps.get(key).setCurrentWeight(NumberUtil.getPoint(currentValue, 2));

            result.add(resultMaps.get(key));
        }

        return result;
    }

    @Override
    public List<AbandonYearVo> getAbandonYearVoList(String year) throws Exception {
        List<AbandonYearVo> abandonYearVos = Lists.newLinkedList();
        for (int i = 1; i <= 12; i++) {
            AbandonYearVo vo = new AbandonYearVo();
            vo.setMoth(i + "月");
            abandonYearVos.add(vo);
        }
        String startTime = year + "-01-01";
        String endTime = year + "-12-31 23:59:59";
        QueryForm queryForm = new QueryForm();
        queryForm.setStartTime(startTime);
        queryForm.setEndTime(endTime);
        List<AbandonMaterial> abandonPreMaterials = abandonMaterialMapper.selectAbandonPercents(queryForm);
        if (CollectionUtils.isEmpty(abandonPreMaterials)) {
            return abandonYearVos;
        }
        Map<String, Double> tempMap = Maps.newHashMap();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
        for (AbandonMaterial abandonMaterial : abandonPreMaterials) {
            String moth = simpleDateFormat.format(abandonMaterial.getCreateTime());
            String whiteDay = abandonMaterial.getWhiteDay();
            String key = Integer.parseInt(moth) + "月-" + whiteDay;
            if (tempMap.containsKey(key)) {
                double v = tempMap.get(key) + Double.parseDouble(abandonMaterial.getWeight());
                tempMap.put(key, v);
            } else {
                tempMap.put(key, Double.parseDouble(abandonMaterial.getWeight()));
            }
        }
        for(AbandonYearVo vo : abandonYearVos){
            String moth = vo.getMoth();
            String baibanKey = moth+"-1";
            String yebanKey = moth+"-2";
            double baibanV = 0.0;
            double yebanV = 0.0;
            if(tempMap.containsKey(baibanKey)){
                baibanV = tempMap.get(baibanKey);
            }
            if(tempMap.containsKey(yebanKey)){
                yebanV = tempMap.get(yebanKey);
            }
            double total = baibanV+yebanV;
            vo.setBaibanValue(NumberUtil.getPoint(baibanV,1));
            vo.setYebanValue(NumberUtil.getPoint(yebanV,1));
            vo.setTotalValue(NumberUtil.getPoint(total,1));
        }
        return abandonYearVos;
    }

    @Override
    public Map<String, Object> getAbandonPercentList(QueryForm queryForm) throws Exception {
        List<AbandonMaterial> abandonPreMaterials = abandonMaterialMapper.selectAbandonPercents(queryForm);
        if (CollectionUtils.isEmpty(abandonPreMaterials)) {
            return Maps.newHashMap();
        }
        Map<String, Object> result = Maps.newHashMap();
        AbandonPercentVo baiban = new AbandonPercentVo();
        AbandonPercentVo yeban = new AbandonPercentVo();

        Map<String, AbandonPercentVo> tempMap = Maps.newHashMap();
        double total = 0.0;
        for (AbandonMaterial abandonMaterial : abandonPreMaterials) {
            String processName = abandonMaterial.getProcessName();
            String abandonType = abandonMaterial.getAbandonType();
            if (!tempMap.containsKey(processName)) {
                AbandonPercentVo percentVo = new AbandonPercentVo();
                percentVo.setName(processName);
                tempMap.put(processName, percentVo);
            }
            String whiteDay = abandonMaterial.getWhiteDay();
            AbandonPercentVo percentVo = tempMap.get(processName);
            total += Double.parseDouble(abandonMaterial.getWeight());
            if (StringUtils.equals(abandonType, "1")) {
                double v = Double.parseDouble(abandonMaterial.getWeight()) + Double.parseDouble(percentVo.getV1());
                percentVo.setV1(NumberUtil.getPoint(v, 1));
                if (StringUtils.equals(whiteDay, "1")) {
                    baiban.setV1((Double.parseDouble(baiban.getV1()) + v) + "");
                } else {
                    yeban.setV1((Double.parseDouble(yeban.getV1()) + v) + "");
                }
            } else if (StringUtils.equals(abandonType, "2")) {
                double v = Double.parseDouble(abandonMaterial.getWeight()) + Double.parseDouble(percentVo.getV2());
                percentVo.setV2(NumberUtil.getPoint(v, 1));
                if (StringUtils.equals(whiteDay, "1")) {
                    baiban.setV2((Double.parseDouble(baiban.getV2()) + v) + "");
                } else {
                    yeban.setV2((Double.parseDouble(yeban.getV2()) + v) + "");
                }
            } else if (StringUtils.equals(abandonType, "3")) {
                double v = Double.parseDouble(abandonMaterial.getWeight()) + Double.parseDouble(percentVo.getV3());
                percentVo.setV3(NumberUtil.getPoint(v, 1));
                if (StringUtils.equals(whiteDay, "1")) {
                    baiban.setV3((Double.parseDouble(baiban.getV3()) + v) + "");
                } else {
                    yeban.setV3((Double.parseDouble(yeban.getV3()) + v) + "");
                }
            } else if (StringUtils.equals(abandonType, "4")) {
                double v = Double.parseDouble(abandonMaterial.getWeight()) + Double.parseDouble(percentVo.getV4());
                percentVo.setV4(NumberUtil.getPoint(v, 1));
                if (StringUtils.equals(whiteDay, "1")) {
                    baiban.setV4((Double.parseDouble(baiban.getV4()) + v) + "");
                } else {
                    yeban.setV4((Double.parseDouble(yeban.getV4()) + v) + "");
                }
            }
        }

        List<AbandonPercentVo> list = Lists.newArrayList();
        AbandonPercentVo totalVo = new AbandonPercentVo();
        totalVo.setV5(NumberUtil.getPoint(total, 1));
        AbandonPercentVo totalPercent = new AbandonPercentVo();
        double v1 = 0.0;
        double v2 = 0.0;
        double v3 = 0.0;
        double v4 = 0.0;
        for (String key : tempMap.keySet()) {
            AbandonPercentVo vo = tempMap.get(key);
            double t = Double.parseDouble(vo.getV1()) + Double.parseDouble(vo.getV2()) + Double.parseDouble(vo.getV3()) + Double.parseDouble(vo.getV4());
            vo.setV5(NumberUtil.getPoint(t, 1));
            String percent = NumberUtil.getPoint((t / total) * 100, 1) + "%";
            vo.setV6(percent);
            list.add(vo);
            v1 += Double.parseDouble(vo.getV1());
            v2 += Double.parseDouble(vo.getV2());
            v3 += Double.parseDouble(vo.getV3());
            v4 += Double.parseDouble(vo.getV4());
        }
        totalVo.setV1(NumberUtil.getPoint(v1, 1));
        totalVo.setV2(NumberUtil.getPoint(v2, 1));
        totalVo.setV3(NumberUtil.getPoint(v3, 1));
        totalVo.setV4(NumberUtil.getPoint(v4, 1));
        String p1 = NumberUtil.getPoint((v1 / total) * 100, 1);
        String p2 = NumberUtil.getPoint((v2 / total) * 100, 1);
        String p3 = NumberUtil.getPoint((v3 / total) * 100, 1);
        String p4 = NumberUtil.getPoint((v4 / total) * 100, 1);
        totalPercent.setV1(NumberUtil.getPoint(p1, 1) + "%");
        totalPercent.setV2(NumberUtil.getPoint(p2, 1) + "%");
        totalPercent.setV3(NumberUtil.getPoint(p3, 1) + "%");
        totalPercent.setV4(NumberUtil.getPoint(p4, 1) + "%");

        double baiV = Double.parseDouble(baiban.getV1()) + Double.parseDouble(baiban.getV2()) + Double.parseDouble(baiban.getV3()) + Double.parseDouble(baiban.getV4());
        String baip = NumberUtil.getPoint((baiV / total) * 100, 1) + "%";
        baiban.setV5(NumberUtil.getPoint(baiV, 1));
        baiban.setV6(baip);

        baiban.setV1(NumberUtil.getPoint(baiban.getV1(),1));
        baiban.setV2(NumberUtil.getPoint(baiban.getV2(),1));
        baiban.setV3(NumberUtil.getPoint(baiban.getV3(),1));
        baiban.setV4(NumberUtil.getPoint(baiban.getV4(),1));

        double yeV = Double.parseDouble(yeban.getV1()) + Double.parseDouble(yeban.getV2()) + Double.parseDouble(yeban.getV3()) + Double.parseDouble(yeban.getV4());
        yeban.setV5(NumberUtil.getPoint(yeV, 1));

        yeban.setV1(NumberUtil.getPoint(yeban.getV1(),1));
        yeban.setV2(NumberUtil.getPoint(yeban.getV2(),1));
        yeban.setV3(NumberUtil.getPoint(yeban.getV3(),1));
        yeban.setV4(NumberUtil.getPoint(yeban.getV4(),1));

        double yp = 100 - (baiV / total) * 100;
        baiban.setV6(NumberUtil.getPoint(yp, 1));

        result.put("baiban", baiban);
        result.put("yeban", yeban);
        result.put("list", list);
        result.put("totalVo", totalVo);
        result.put("totalPercent", totalPercent);

        return result;
    }

    @Override
    public String addBatchAbandonMaterialHandles(List<AbandonMaterialHandle> abandonMaterialHandles) throws Exception {
        if (CollectionUtils.isEmpty(abandonMaterialHandles)) {
            return "参数异常！";
        }
        for (AbandonMaterialHandle handle : abandonMaterialHandles) {
            handle.setId(CommonUtil.generateRandomNum("handle-"));
            String outWight = handle.getOutWeight();
            outWight = NumberUtil.getPoint(outWight, 2);
            handle.setOutWeight(outWight);
        }
        int count = abandonMaterialHandleMapper.batchInsertAbandonHandle(abandonMaterialHandles);
        return CommonUtil.outStr(count);
    }
}
